package com.longrise.android.byjk.plugins.tabfirst.commentdialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.utils.NetUtil;


/**
 * Created by sujizhong on 16/8/26.
 */
public class ComDialog extends Dialog {

    private static final String TAG = "ComDialog";

    private InputBackEditText mEditComment;
    private TextView mTextSend;

    private Context mCxt;

    private MsgCallback mMsgCallback = null;

    public ComDialog(Context context, MsgCallback msgCallback) {
        super(context, R.style.VideoListDialog);
        mCxt = context;
        mMsgCallback = msgCallback;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.dialogc_comment);
        fullWidth();
        init();

//        mEditComment.setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
//                return false;
//            }
//        });
    }

    private void fullWidth() {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = getWidth(mCxt);
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;  //-2
        this.getWindow().setAttributes(lp);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void init() {
        mEditComment = (InputBackEditText) findViewById(R.id.editcomment);
        mEditComment.setInputBackListener(mInputBackListener);
        mEditComment.addTextChangedListener(mTextWatcher);
        mTextSend = (TextView) findViewById(R.id.textcommentupload);
        mTextSend.setOnClickListener(mOnClickListener);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.textcommentupload:
                    String msg = mEditComment.getText().toString().trim();
                    if(TextUtils.isEmpty(msg)){
                        return;
                    }
                    if (checkNetwork() && mMsgCallback != null) {
                        mMsgCallback.msg(msg);
                        dismiss();
                    }
                    break;

                default:
                    break;
            }
        }
    };

    private InputBackEditText.InputBackListener mInputBackListener = new InputBackEditText.InputBackListener() {

        @Override
        public void back(TextView textView) {
            if (isShowing()) dismiss();
        }
    };

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            try {
                String msg = mEditComment.getText().toString().trim();
                if (msg.length() <= 0) {
                    switchClickAble(false, R.drawable.shape_hui);
                } else {
                    switchClickAble(true, R.drawable.shape_green);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private void switchClickAble(boolean b, int resourceD) {
        mTextSend.setEnabled(b);
        mTextSend.setClickable(b);
        mTextSend.setBackgroundResource(resourceD);
    }

    public static int getWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    private boolean checkNetwork() {
        if (!NetUtil.checkNetEnable()) {
            return false;
        } else {
            return true;
        }
    }

    public interface MsgCallback {
        void msg(String msg);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mMsgCallback = null;
        mEditComment = null;
        mCxt = null;
    }
}
